<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2209 – Use `var=$(command)` to assign output (or quote to assign string).</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2209 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2209">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="use-varcommand-to-assign-output-or-quote-to-assign-string">Use
<code>var=$(command)</code> to assign output (or quote to assign
string).</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2209.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="va">user</span><span class="op">=</span>whoami         <span class="co"># Want to run whoami and assign output</span></span>
<span id="cb1-2"><a href="SC2209.html#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="SC2209.html#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="va">PAGER</span><span class="op">=</span>cat <span class="fu">git</span> log   <span class="co"># Want to assign the string &quot;cat&quot;</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2209.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="va">user</span><span class="op">=</span><span class="va">$(</span><span class="fu">whoami</span><span class="va">)</span></span>
<span id="cb2-2"><a href="SC2209.html#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-3"><a href="SC2209.html#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="va">PAGER</span><span class="op">=</span><span class="st">&quot;cat&quot;</span> <span class="fu">git</span> log</span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>Putting <code>var=</code> in front of a command will not assign its
output. Use <code>var=$(my command here)</code> to execute the command
and capture its output.</p>
<p>If you do want to assign a literal string, use quotes to make this
clear to shellcheck and humans alike.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>None.</p>
<p>Quoting a single command (as in <code>PAGER="cat"</code> above)
doesn't change how the script works. It's purely to show shellcheck (and
humans) that a literal assignment of a command name is intentional.</p>
<p>This warning triggers generally when a variable is assigned an
unquoted command name (from a list of hard coded names). See related
warning <a href="SC2037.html">SC2037</a> which detects the same kind of error
through the patterns <code>var=value -flag</code> and
<code>var=value *glob*</code>.</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li><a href="https://mywiki.wooledge.org/BashFAQ/002">BashFaq: How can I
store the return value and/or output of a command in a
variable?</a></li>
<li><a
href="https://stackoverflow.com/questions/4651437/how-to-set-a-variable-to-the-output-from-a-command-in-bash">StackOverflow:
How to set a variable to the output from a command in Bash</a></li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


